$.Class("Monitor.Grid",
// static properties
{     
    addParams : function(e, params)
    {
        e.sender.transport.options.read.data = params;
    }    
},
// prototype properties
    {
        // constructor function
        init: function(id) {
            this.id = id;
            
            this.dataSource = this.createDataSource();
            this.createGrid();     
            this.initEvents(id);
        },
        createDataSource: function()
        {
            var dataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: "/monitor/search",
                        dataType: "json",
                        type: "post",
                        data : {
                            
                        }
                    }
                },
                pageSize: 25,
                serverPaging: true,
                serverFiltering: true,
                serverSorting: true,
                schema: {
                    data: "results",
                    total: function(data) {
                        return data.total;
                    }
                },
                requestStart: function(e) {
                    var data = {
                        domain : $("#_domain").val(),
                        isAjax : true
                    };
                    
                    Monitor.Grid.addParams(e, data);
                }
            });

            return dataSource;
        }, 
        createGrid : function()
        {
            $("#" + this.id).kendoGrid({
	        dataSource: this.dataSource,
	        toolbar: ["Reset"],
	        //height: 500,
	        filterable: false,
	        sortable: false,
	        pageable: true,
	        columns: [{
                field:"id",
                title:"ID",
                width: 40
            },{
                field:"grid_index",
                title:" ",
                width: 40
            },{
                field:"domain",
                title:"Domain",
                width: 200
            },{
                field:"hyipNumber",
                title:"Hyip number",
                width: 100
            },{
                field:"alexaRank",
                title:"Alexa rank",
                width: 100
            },{
                field:"rate",
                title:"Rate",
                width: 120,
                template: '<div id="rate_${id}" itemID="${id}" value="${rate}" class="rate"></div>'
            },{
            	title:"",
                width: 60,
                template: '<a href="" onclick="return showWindowNote(${id});">Note</a>'
            },{
            	title:"",
                width: 45,
                template: '<a href="" onclick="return Monitor.edit(${id});">Edit</a>'
            },{
            	title:"",
                field:"",         
                width: 60,
                template: '<a href="" onclick="return Monitor.delete(${id});">Delete</a>'
            },{
                field:"",
                title:"",             
            }],
            editable:false,
            sortable:true,
            selectable: 'row',
            dataBound: function(e) {      
                $(".rate").each(function(){                   
                    var itemID = $(this).attr("itemID"); 
                    var rate = $(this).attr("value");
                    
                    if(rate == null) rate = 0;
                    
                    $('#' + this.id).raty({
                        half  : true,
                        score : rate,
                        click: function(score, evt) {
                            $.ajax({
                                type: 'post',
                                url: '/monitor/updateRate',
                                data: {
                                    id : itemID,
                                    rate : score
                                },
                                dataType: 'json',
                                success: function(obj){

                                },
                            });
                        }
                    });
                });
            }
        });	
        },
        initEvents : function(id)
        {
            $(".k-grid-Reset", "#" + this.id).click(function(){
                $("#" + id).data("kendoGrid").dataSource.read();	// Reset grid
                return false;
            });	
        },
        refresh : function()
        {
            $("#" + this.id).data("kendoGrid").dataSource.read();	// Reset grid
        }
})

Monitor.create = function()
{
    $.ajax({
        type: 'post',
        url: '/monitor/create',
        data: {
            domain : $("#domain").val(),
            alexaRank : $("#alexaRank").val(),
            note : $("<div />").html($("#note").val()).text()
        },
        dataType: 'json',
        success: function(obj){
            //$("#grid").data("kendoGrid").dataSource.read();
            document.location = "/monitor/index";
        },
    });
}

Monitor.update = function()
{
    $.ajax({
        type: 'post',
        url: '/monitor/update',
        data: {
            id : $("#monitorID").val(),
            domain : $("#domain").val(),
            alexaRank : $("#alexaRank").val(),
            note : $("<div />").html($("#note").val()).text()
        },
        dataType: 'json',
        success: function(obj){
            //$("#grid").data("kendoGrid").dataSource.read();
            document.location = "/monitor/index";
        },
    });
}

Monitor.delete = function(id)
{
    if(confirm("Do you want to delete?"))
    {
        $.ajax({
            type: 'post',
            url: '/monitor/delete',
            data: {
                id : id
            },
            dataType: 'json',
            success: function(obj){
                $("#grid").data("kendoGrid").dataSource.read();
            },
        });
    }
    
    return false;
}

Monitor.edit = function(id)
{
    tabs.data("kendoTabStrip").select(1);
    
    $.ajax({
        type: 'post',
        url: '/monitor/getItem',
        data: {
            id : id
        },
        dataType: 'json',
        success: function(obj){
            $("#monitorID").val(obj.id);
            $("#domain").val(obj.domain);
            $("#note").data("kendoEditor").value(obj.note);
            $("#alexaRank").data("kendoNumericTextBox").value(obj.alexaRank);
            
            $("#btnCreate").hide();
            $("#btnUpdate").show();
            
            $("html, body").animate({ scrollTop: 0 }, "fast");
        },
    });
    
    return false;
}